﻿Imports System.Data
Imports System.Data.Odbc

Public Class Ejecutor
    Public Function EjecutaQuery(ByVal query As String, ByVal archivo As String) As Integer
        'tipo=0 : Consulta tipo=1 : Inserta tipo=2 : Actualiza tipo=3 : Elimina Registros

        Dim sqlComm As OdbcCommand
        Dim sConn As String
        sConn = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" & System.IO.Path.GetDirectoryName(archivo) & ";"

        Using dbConn As New System.Data.Odbc.OdbcConnection(sConn)
            Try
                dbConn.Open()
                sqlComm = New OdbcCommand(query, dbConn)

                Return sqlComm.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox("Error al abrir la base de datos " & vbCrLf & ex.Message)
                Return Nothing
            Finally
                dbConn.Close()
            End Try
        End Using
    End Function
    Public Function ExisteQuery(ByVal query As String, ByVal archivo As String) As Boolean
        'tipo=0 : Consulta tipo=1 : Inserta tipo=2 : Actualiza tipo=3 : Elimina Registros

        Dim sqlComm As OdbcCommand
        Dim sConn As String
        sConn = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" & System.IO.Path.GetDirectoryName(archivo) & ";"

        Using dbConn As New System.Data.Odbc.OdbcConnection(sConn)
            Try
                dbConn.Open()
                sqlComm = New OdbcCommand(query, dbConn)

                'Dim i As Integer = 0
                'Dim reader As OdbcDataReader = sqlComm.ExecuteReader()

                'While reader.Read()
                '    MsgBox(reader.GetString(1))
                'End While

                Return sqlComm.ExecuteReader().HasRows
            Catch ex As Exception
                MsgBox("Error al abrir la base de datos " & vbCrLf & ex.Message)
                Return False
            Finally
                dbConn.Close()
            End Try
        End Using
    End Function
End Class
